package logic

import (
	"context"
	"fmt"
	"models"

	"shop_srv/internal/svc"
	"shop_srv/shop_srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsPushLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsPushLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsPushLogic {
	return &GoodsPushLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsPushLogic) GoodsPush(in *shop_srv.GoodsPushRequest) (*shop_srv.GoodsPushResponse, error) {
	// todo: add your logic here and delete this line
	if in.Price <= 0 {
		return nil, fmt.Errorf("价格不得小于等于0")
	}
	if in.Stock <= 0 {
		return nil, fmt.Errorf("存货量不得小于等于0")
	}
	if in.Max < 1 {
		return nil, fmt.Errorf("最大购买量不得小于1")
	}
	goods := models.Goods{
		GoodsName:   in.Name,
		GoodsPrice:  in.Price,
		GoodsStock:  in.Stock,
		GoodsNumMax: in.Max,
		UserId:      in.UserId,
	}
	if err := models.DB.Create(&goods).Error; err != nil {
		return nil, err
	}
	return &shop_srv.GoodsPushResponse{}, nil
}
